package com.agrm.category.controller;

import com.agrm.category.service.CategoryService;
import com.agrm.model.category.pojo.Category;
import com.agrm.model.common.dtos.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/api/v1/category")
public class CategoryController {

    //TODO 分类Redis缓存没加

    @Autowired
    private CategoryService categoryService;

    /**
     * 根据id查询单个分类（不包含子分类）
     * @return
     */
    @GetMapping("/get")
    public ResponseResult getById(@RequestParam("id") Integer id){
        Category category = categoryService.getById(id);
        return ResponseResult.okResult(category);
    }


    /**
     * 根据id查询分类树
     * @param id id
     * @return 分类树
     */
    @GetMapping("/getTree")
    public ResponseResult getTreeById(@RequestParam(defaultValue = "1") Integer id){
        Category category = categoryService.getTreeById(id);
        return ResponseResult.okResult(category);
    }

    /**
     * 根据父分类ID获取直接子分类（只查询下一级，不递归）
     * @param parentId 父分类ID
     * @return 直接子分类列表
     */
    @GetMapping("/getChildren")
    public ResponseResult getChildrenByParentId(@RequestParam("parentId") Integer parentId){
        List<Category> children = categoryService.getChildrenByParentId(parentId);
        return ResponseResult.okResult(children);
    }

    /**
     * 获取所有一级分类（parent_id为1的分类）
     * @return 一级分类列表
     */
    @GetMapping("/getFirstLevel")
    public ResponseResult getFirstLevelCategories(){
        List<Category> firstLevelCategories = categoryService.getFirstLevelCategories();
        return ResponseResult.okResult(firstLevelCategories);
    }


    /**
     * 查询所有分类 (非树形结构)
     * @return
     */
    @GetMapping("/list")
    public ResponseResult list(){
        return ResponseResult.okResult(categoryService.list());
    }


    /**
     * 获取所有分类树
     * @return
     */
    @GetMapping("/listTree")
    public ResponseResult listTree(){
        //根节点可能有多个
        List<Category> lists = categoryService.listTree();
        return ResponseResult.okResult(lists);
    }


    /**
     * 修改分类
     * @param category
     * @return
     */
    @PutMapping("update")
    public ResponseResult update(@RequestBody Category category){
        category.setUpdatedTime(LocalDateTime.now());
        boolean update = categoryService.updateById(category);
        return ResponseResult.okResult(update);
    }

    /**
     * 添加分类
     * @param category
     * @return
     */
    @PostMapping("add")
    public ResponseResult add(@RequestBody Category category){
        category.setCreatedTime(LocalDateTime.now());
        category.setUpdatedTime(LocalDateTime.now());
        return ResponseResult.okResult(categoryService.save(category));
    }

    /**
     * 删除分类
     * @param ids
     * @return
     */
    @DeleteMapping("delete")
    public ResponseResult delete(@RequestBody List<Integer> ids){
        boolean res = categoryService.deleteByIds(ids);
        if(res){
            return ResponseResult.okResult("成功删除");
        }
        return ResponseResult.okResult("存在分类含有子分类，删除失败");
    }


}
